<template>
    <a-modal :width="550"
             :title="title"
             destroyOnClose
             :maskClosable="false"
             v-model="visible">
        <a-form-model
                ref="ruleForm"
                small
                :model="form"
                :rules="rules"
                :label-col="labelCol"
                :wrapper-col="wrapperCol">
            <a-form-model-item label="角色名称" prop="roleName">
                <a-input placeholder="请输入角色名称" v-model="form.roleName" />
            </a-form-model-item>
            <a-form-model-item label="角色编码" prop="roleCode">
                <a-input placeholder="请输入角色编码" v-model="form.roleCode" />
            </a-form-model-item>
        </a-form-model>
        <div slot="footer" class="text-c">
            <a-button @click="close">关闭</a-button>
            <a-button type="primary" :loading="loading" @click="sure">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    export default {
        data () {
            return {
                title: '新增角色',
                form: {
                    id: '',
                    roleName: '',
                    roleCode: '',
                },
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
                rules: {
                    roleName: [
                        { required: true, message: '请输入角色名称', trigger: 'blur' },
                    ],
                    roleCode: [
                        { required: true, message: '请输入角色编码', trigger: 'blur' },
                    ],
                },
                actionType: null,
                visible: false,
                loading: false,
            };
        },
        watch: {
            visible (val) {
                if (!val) {
                    this.close();
                }
            }
        },
        methods: {
            sure () {
                this.$refs.ruleForm.validate(valid => {
                    if (valid) {
                        let tipMsg = this.actionType === 0 ? '新增成功' : '编辑成功';
                        let api = this.actionType === 0 ? this.$api.permission.addrole : this.$api.permission.editrole;
                        api(this.form).then(res => {
                            if (res && res.code !== 200) {
                                this.$message.error(res.errorMsg);
                                return;
                            }
                            this.$message.success(tipMsg);
                            this.$emit('done');
                            this.close();
                        }).catch(err => {
                            this.$message.error(err.errorMsg);
                        });
                    }
                });
            },
            show (data,title,actionType) {
                this.title = title;
                this.form = {
                    id: data.id || '',
                    roleName: data.roleName || '',
                    roleCode: data.roleCode || '',
                };
                this.actionType = actionType;
                this.visible = true;
            },
            close () {
                this.form = {
                    id: '',
                    roleName: '',
                    roleCode: '',
                };
                this.title = '';
                this.actionType = null;
                this.loading = false;
                this.visible = false;
            }
        }
    };
</script>

<style scoped lang="scss">

</style>
